using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;

namespace Ent.SqlDB
{
    public class Connection
    {
        static SqlConnection _conn = null;
        public static SqlConnection Get(string connectionString)
        {
            if (_conn == null)
            {
                _conn = new SqlConnection(connectionString);
            }
            Open();
            return _conn;
        }
        public static SqlConnection Get()
        {
            if (_conn == null)
            {
                try
                {
                    return Ent.SqlDB.Connection.Get(ConfigurationManager.AppSettings.Get("ConnectionString"));
                }
                catch (Exception exc)
                {
                    throw new Exception("Connection string required to make database connection.");
                }
            }
            Open();
            return _conn;
        }
        private static void Open()
        {
            try
            {
                if (_conn.State != System.Data.ConnectionState.Open)
                    _conn.Open();
            }
            catch (Exception exc)
            {
                throw new Exception("Unable to connect the database. " + exc.Message);
            }
        }
        public static void Set(string connectionString)
        {
            Get(connectionString);
        }
    }
}
